<template>
  <el-dialog
    title="批量设置检查分组"
    :visible.sync="visible"
    :destroy-on-close="true"
    width="400px"
    :before-close="handleClose">
    <div class="dlg_wrap">
      <el-form class="rule-form" ref="form" inline :model="ruleForm">
        <el-form-item label="检查分组" prop="checkGroup" class="short-input">
          <el-select
            style="width:100%"
            size="small"
            v-model="ruleForm.checkGroup"
            placeholder="请选择"
            clearable
            filterable
            allow-create
          >
            <el-option
              v-for="item in checkGroupOptions"
              :key="item"
              :label="item"
              :value="item"
            ></el-option>
          </el-select>
        </el-form-item>
      </el-form>
      <div class="form_tips">请确认，已选择 <b class="fc_red">{{ids.length}}</b> 个部位，批量设置检查分组</div>
    </div>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="sendSet">提交</el-button>
    </span>
  </el-dialog>
</template>

<script>
import {
  getCheckGroupList,
  setCheckGroup
} from '@/api/api.js'
export default {
  props: {
    dialogVisibleCheckGroup: {
      type: Boolean,
      default: false
    },
    hospitalCode: {
      type: String,
      default: ''
    },
    departCode: {
      type: Array,
      default: () => []
    },
    ids: {
      type: Array,
      default: () => []
    }
  },
  data() {
    return {
      ruleForm: {
        checkGroup: ''
      },
      checkGroupOptions: []
    }
  },
  computed: {
    visible: {
      get() {
        return this.dialogVisibleCheckGroup
      },
      set(val) {
        this.$emit('update:dialogVisibleCheckGroup', val)
      }
    }
  },
  watch: {
    visible(val) {
      if (val) {
        this.ruleForm.checkGroup = ''
        this.getCheckGroupOptions()
      }
    },
    hospitalCode(val) {
      if (val && this.visible) {
        this.getCheckGroupOptions()
      }
    }
  },
  methods: {
    handleClose() {
      this.visible = false
    },
    // 提交设置
    sendSet() {
      const _ids = this.ids.map(item => item.id)
      const params = {
        hospitalCode: this.hospitalCode,
        ids: _ids,
        checkGroup: this.ruleForm.checkGroup
      }
      setCheckGroup(params).then(res => {
        if (res) {
          this.$message.success(res.message || '设置成功')
          this.visible = false
          this.$emit('refresh')
        }
      })
    },
    /* 数据字典下项目列表 */
    getCheckGroupOptions() {
      if (this.checkGroupOptions.length > 0) {
        this.checkGroupOptions = []
      }
      const params = {
        hospitalCode: this.hospitalCode,
        dep: this.departCode.join(',')
      }
      return new Promise(resolve => {
        getCheckGroupList(params).then((res) => {
          if (res.code === 10000) {
            this.checkGroupOptions = res.content.list
            resolve(res)
          }
        })
      })
    }
  }
}
</script>

<style lang="scss" scoped>
.form_tips{
  font-size: 12px;
  color: #333;
}
</style>
